package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IFilePathDao;
import com.boss.salary.entity.FilePath;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class FilePathDaoImpl extends BaseDaoImpl implements IFilePathDao {


    @Override
    public void insertFilePath(FilePath filePath) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(TableName.SAL_FILE_PATH_CONFIG);
        sql.append("(id, bank_code,bank_name,upload_dir_path, download_dir_path, mof_div_code)")
                .append(" value (?,?,?,?,?,?)");
        Object[] params = new Object[]{
                filePath.getId(),filePath.getBankCode(),filePath.getBankName(),filePath.getUploadDirPath(),
                filePath.getDownloadDirPath(),filePath.getMofDivCode()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void deleteFilePath(String id, String mofDivCode) {
        StringBuilder sql = new StringBuilder();
        sql.append("delete  from ").append(TableName.SAL_FILE_PATH_CONFIG);
                sql.append("  where id=? and mof_div_code=? ");
        Object[] params = new Object[]{
                id, mofDivCode
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void updateFilePath(FilePath filePath) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TableName.SAL_FILE_PATH_CONFIG);
        sql.append(" set  bank_code = ?, bank_name = ?, upload_dir_path = ?, download_dir_path = ?")
                .append(" WHERE id = ? and mof_div_code = ?");
        Object[] params = new Object[]{
                filePath.getBankCode(),filePath.getBankName(),filePath.getUploadDirPath(),filePath.getDownloadDirPath()
                ,filePath.getId(),filePath.getMofDivCode()
        };
        this.getjdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public List<FilePath> queryFilePath(PaginationDTO paginationDTO, String province) {
        StringBuilder sql = new StringBuilder();
        List<Object> Params = new ArrayList<>();
        sql.append("select  bank_code,bank_name, upload_dir_path, download_dir_path, mof_div_code")
               .append(" from ")
                .append(TableName.SAL_FILE_PATH_CONFIG);
        sql.append(" where  mof_div_code=?");
        Params.add(province);
        sql.append(" order by bank_code");
        String querySql = this.boxUpSql(sql.toString());
        //请求参数初始化
        Object[] params = this.boxUpParam(Params.toArray(), paginationDTO);
        return this.getJdbcTemplate().query(querySql, new BeanPropertyRowMapper(FilePath.class), params);
    }

    @Override
    public int queryFilePathTotal(String province) {
        StringBuilder sql = new StringBuilder();
        sql.append("select count(*) from ").append(TableName.SAL_FILE_PATH_CONFIG)
                .append(" where mof_div_code = ? ");
        return this.getJdbcTemplate().queryForObject(sql.toString(),Integer.class, province);
    }
}
